package ua.org.nuos.sdms.clientgui.client.components.tabs;

import com.vaadin.ui.Button;
import com.vaadin.ui.Component;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 18.03.12
 * Time: 18:41
 * To change this template use File | Settings | File Templates.
 */
public abstract class CustomTab implements ICustomTab {

    public enum State {
        CLOSED, OPENED
    }

    protected String caption;
    protected Button tabButton;
    protected int index;
    protected boolean isDataViewer;
    protected boolean isInitialized;
    protected boolean isSelected;

    protected CustomTab(String caption) {
        this.caption = caption;
        initButton();
    }

    protected void initButton() {
        if ((caption == null) || (caption.isEmpty())) {
            caption = "tab " + index;
        }
        tabButton = new Button(caption);
        tabButton.setStyleName("custom_tab_active");
        tabButton.addStyleName("text_float_left");
        tabButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent clickEvent) {
                select();
            }
        });
        tabButton.setWidth("100%");
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        if (isInitialized) {
            throw new IllegalStateException("Can not set tab index after component initialization");
        }
        this.index = index;
        isInitialized = true;
    }

    public void setButtonWidth(String width) {
        tabButton.setWidth(width);
    }

    public boolean isDataViewer() {
        return isDataViewer;
    }

    public boolean isSelected() {
        return isSelected;
    }

    @Override
    public void deselect() {
        if (isSelected) {
            tabButton.removeStyleName("custom_tab_inactive");
            tabButton.setStyleName("custom_tab_active");
            tabButton.addStyleName("text_float_left");
            isSelected = false;
        }
    }

    public Component getButton() {
        return tabButton;
    }
}

